<?php

class DocumentController extends \BaseController {

    /**
     * 获取内容列表
     * @param  [id] $categoryId [栏目id]
     * @return [array]             [栏目id、栏目名、文档内容]
     */
    public function index(){
        $categoryId = Input::get('id');
        $data       = array('id' => $id);
        $rules      = array('id' => 'required | exists:category,id');
        BaseService::validateInputParam($data, $rules);
        
        $documents = DocumentService::index($id);
        return View::make('document.index')->with('documents', $documents);
    }

    /**
     *  进入文档增加页面
     * @param  [int] $id [栏目id]
     * @return [array]             [栏目信息，模板字段信息]
     */
    public function create(){
        $id = Input::get('id');
        $data       = array('id' => $id);
        $rules      = array('id' => 'required | exists:category,id');
        BaseService::validateInputParam($data, $rules);
        
        $document = DocumentService::create($id);
        return $document;
    }

    /**
     * 增加文档
     * @param [idt] $categoryId [栏目id]
     */
    public function add(){
        $inputParam = Input::all();
        $categoryId = Input::get('categoryId');
        $data       = array('categoryId' => $categoryId);
        $rules      = array('categoryId' => 'required | exists:category,id');
        BaseService::validateInputParam($data, $rules);
        
        $result = DocumentService::add($categoryId, $inputParam);
        return $result;
    }
    
    /**
     * 显示文档详情
     * @return 文档信息
     * 
     */
    public function show(){
        $id         = Input::get('id');
        $categoryId = Input::get('categoryId');
        $data       = array('categoryId'    => $categoryId, 'id' => $id);
        $rules      = array('categoryId'    => 'required | exists:category,id',
                             'id'           => 'required | integer');
        BaseService::validateInputParam($data, $rules);
        

        $documents = DocumentService::show($id, $categoryId);
        return View::make('document.show')->with('documents', $documents);
    }

    /**
     * 进入文档更改页面
     * @param  [int] $id         [文档id]
     * @param  [int] $categoryId [栏目id]
     * @return [array]             [文档信息，模板字段]
     */
    public function edit(){
        $id         = Input::get('id');
        $categoryId = Input::get('categoryId');
        $data       = array('categoryId'    => $categoryId, 'id' => $id);
        $rules      = array('categoryId'    => 'required | exists:category,id',
                             'id'           => 'required | integer');
        BaseService::validateInputParam($data, $rules);
        
        $documents = DocumentService::show($id, $categoryId);
        return View::make('document.update')->with('documents', $documents);
    }

    /**
     * 更新文档数据
     * @param  [int] $id         [文档id]
     * @param  [int] $categoryId [栏目id]
     * @return []             []
     */
    public function update(){
        $data = Input::all();
        $rules = array('categoryId' => 'required | exists:category,id',
                             'id'        => 'required | integer');
        BaseService::validateInputParam($data, $rules);
        
        $result = DocumentService::update($data);
        return $result;
    }

    /**
     * 删除文档
     * @param  [int] $id         [文档id]
     * @param  [int] $categoryId [栏目id]
     */
    public function delete(){
        $data = Input::all();
        $rules = array('categoryId' => 'required | exists:category,id',
                             'id'        => 'required | integer');
        BaseService::validateInputParam($data, $rules);
        
        $result = DocumentService::delete($data);

        return $result;
    }

}